package com.tj.seed.common.domain;

import com.tj.seed.common.constant.ResultCode;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;

import java.util.Objects;

@ApiModel(value = "统一返回")
@Data
@Accessors(chain = true)
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class Result<T> {
    @ApiModelProperty(value = "返回码", example = "200")
    private Integer code;
    @ApiModelProperty(value = "异常信息", example = "无权操作")
    private String errMsg;
    private T data;

    public static Result fail(ResultCode resultCode) {
        return Result.builder().code(resultCode.getCode()).errMsg(resultCode.getErrMsg()).build();
    }

    public static Result fail(String errMsg) {
        return Result.builder().code(ResultCode.FAILED.getCode()).errMsg(errMsg).build();
    }

    public static Result success() {
        return Result.builder().code(ResultCode.SUCCESS.getCode()).errMsg(ResultCode.SUCCESS.getErrMsg()).build();
    }
    public static <T> Result success(T t){
        return Result.builder().code(ResultCode.SUCCESS.getCode()).data(t).build();
    }
    public boolean isSuccess(){
        return Objects.equals(code,ResultCode.SUCCESS.getCode());
    }
}
